<?php
$loader->requireOnce('includes/Utils.class.php');
/**
 * Object Relational Persistence Mapping Class for table: insurance
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class Insurance extends ORDataObject {

	/**#@+
	 * Fields of table: insurance mapped to class members
	 */
	var $insurance_id			=	'';
	var $patient_id				= 	'';
	var $insurance_code			= 	'';
	var $insurance_type			= 	'';
	var $date_issued			= 	'';
	var $date_expired			= 	'';
	var	$registered_hospital_id	=	'';
	var $correct_route          =   '';
	var $paid_for_36_month      =   '';
	/**#@-*/

	const 	REQUIRED 			= 	"required";
	const 	VOLUNTEERED 		= 	"volunteered";

	/**
	 * DB Table
	 */
	var $_table = 'insurance';

	/**
	 * Primary Key
	 */
	var $_key = 'insurance_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'Insurance';

	/**
	 * Handle instantiation
	 */
	function Insurance() {
		parent::ORDataObject();
	}

	public function setData($patient_id, $insurance_code,
		  $date_issued, $date_expired, $registered_hospital_id,
		  $correct_route, $paid_for_36_month){
		$data	= array(
			'patient_id'				=>	$patient_id,
			'insurance_code'			=>	$insurance_code,
			'date_issued'				=>	$date_issued,
			'date_expired'				=>	$date_expired,
			'registered_hospital_id'	=>	$registered_hospital_id,
		    'correct_route'             =>  $correct_route,
		    'paid_for_36_month'         =>  $paid_for_36_month
		);
		$this->populateArray($data);
	}

	public function fromPatientId($patientId) {
		settype($patientId, 'int');
		$ins = Celini::newORDO('Insurance');
        
		$sql = "SELECT insurance_id FROM insurance WHERE patient_id = $patientId ORDER BY date_expired DESC LIMIT 1";
		$db = Celini::dbInstance();
		$res = $db->Execute($sql);
		
		if (!empty($res) && !$res->EOF) {
		    $ins->setup(intval($res->fields['insurance_id']));
		}
		return $ins;
	}
	
    public function checkExist($patientId, $insuranceCode, $dateIssued, $dateExpired) {
        $sql    =   "SELECT $this->_key FROM $this->_table
                    WHERE patient_id = '$patientId' AND insurance_code = '$insuranceCode'
                        AND date_issued= '$dateIssued' AND date_expired = '$dateExpired'";
        $res    =   $this->_execute($sql);
        
        if($res && !$res->EOF) {
            return true;
        }
        return false;
    }
    
	function insuranceType($patientId) {
		$ins = Celini::newORDO('Insurance');

		$res = $ins->_execute("
			select insurance_type
			from $ins->_table
			where patient_id = $patientId AND
				date_expired >= NOW()");
		if ($res && !$res->EOF) {
			return $res->fields['insurance_type'];
		}
		return false;
	}
	
	function delete($patientId) {
	    $sql = "DELETE FROM $this->_table WHERE patient_id = $patientId";
        return $this->_execute($sql);
	}

	/**#@+
	 * Field: date_issued, time formatting
	 */
	function getIssuedDate() {
        return $this->getLocalizedDate('date_issued', 'medium');
	}

	function setIssuedDate($date) {
        $this->date_issued=$date;
	}
	/**#@-*/

	/**#@+
	 * Field: date_expired, time formatting
	 */
	function getExpiredDate() {
        return $this->getLocalizedDate('date_expired', 'medium');
	}

	function setExpiredDate($date) {
        $this->date_expired=$date;
	}

    function setId($id){
        $this->insurance_id=$id;
    }

    function setPatientId($id){
        $this->patient_id=$id;
    }

    public function isExistPatient($patientId) {
    	$sql = "select * from $this->_table where patient_id = $patientId and insurance_code != '' and DATE(date_expired) >= CURRENT_DATE()";
    	$res = $this->_execute($sql);
    	if ($res && !$res->EOF) {
    		return true;
    	}
    	return false;
    }

	public function getRegisteredHospitalName(){
		$hospital	=	Celini::newORDO('Hospital',$this->registered_hospital_id);
		return $hospital->getLocalizedText('name');
	}
	
	public function getAvailableInsuranceByPatient($patientId) {
		$format   =   L10nHelper::getSqlDateFormat();
        $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$sql  =   array(
            'cols'  =>  "insurance_id, insurance_code, DATE_FORMAT(date_issued, '$format') date_issued,
		              DATE_FORMAT(date_expired, '$format') date_expired, correct_route, paid_for_36_month,
		              h.$field hospital_name",
            'from'  =>  "$this->_table
                        LEFT JOIN hospital h ON (registered_hospital_id = h.hospital_id)",
            'where' =>  "patient_id = '$patientId'
                        AND DATE(date_expired) >= CURRENT_DATE()",
            'orderby'   =>  "date_issued DESC"
		);
		$ds = new Datasource_sql();
		$ds->setup($this->_db, $sql);
        return $ds->toArray();
	}
}
?>
